<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Binds an HTTP server on the specified address and port</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="eventhttp.addserveralias.html">« EventHttp::addServerAlias</a></li>
      <li style="float: right;"><a href="eventhttp.construct.html">EventHttp::__construct »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.eventhttp.html">EventHttp</a></li>
    <li>Binds an HTTP server on the specified address and port</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="eventhttp.bind" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">EventHttp::bind</h1>
  <p class="verinfo">(PECL event &gt;= 1.2.6-beta)</p><p class="refpurpose"><span class="refname">EventHttp::bind</span> &mdash; <span class="dc-title">Binds an HTTP server on the specified address and port</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-eventhttp.bind-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span>
   <span class="methodname"><strong>EventHttp::bind</strong></span>(<span class="methodparam">
    <span class="type">string</span>
     <code class="parameter">$address</code>
   </span>, <span class="methodparam">
    <span class="type">int</span>
     <code class="parameter">$port</code>
   </span>): <span class="type"><span class="type void">void</span></span></div>

  <p class="para rdfs-comment">
   Binds an HTTP server on the specified address and port.
  </p>
  <p class="para">
   Can be called multiple times to bind the same HTTP server to multiple
   different ports.
  </p>
 </div>

 <div class="refsect1 parameters" id="refsect1-eventhttp.bind-parameters">
  <h3 class="title">参数</h3>
  <dl>
   
    <dt>

     <code class="parameter">address</code>
    </dt>

    <dd>

     <p class="para">
      A string containing the IP address to
      <code class="literal">listen(2)</code>
      on.
     </p>
    </dd>

   
   
    <dt>

     <code class="parameter">port</code>
    </dt>

    <dd>

     <p class="para">
      The port number to listen on.
     </p>
    </dd>

   
  </dl>

 </div>

 <div class="refsect1 returnvalues" id="refsect1-eventhttp.bind-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功时返回 <strong><code>true</code></strong>， 或者在失败时返回 <strong><code>false</code></strong>。
  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-eventhttp.bind-examples">
  <h3 class="title">范例</h3>
  <div class="example" id="example-4250">
   <p><strong>示例 #1 
    <span class="function"><strong>EventHttp::bind()</strong></span> example</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$base&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">EventBase</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$http&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">EventHttp</span><span style="color: #007700">(</span><span style="color: #0000BB">$base</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$socket&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">socket_create</span><span style="color: #007700">(</span><span style="color: #0000BB">AF_INET</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">SOCK_STREAM</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">SOL_TCP</span><span style="color: #007700">);<br /><br />if&nbsp;(!</span><span style="color: #0000BB">$http</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">bind</span><span style="color: #007700">(</span><span style="color: #DD0000">"127.0.0.1"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">8088</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;exit(</span><span style="color: #DD0000">"bind(1)&nbsp;failed\n"</span><span style="color: #007700">);<br />};<br />if&nbsp;(!</span><span style="color: #0000BB">$http</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">bind</span><span style="color: #007700">(</span><span style="color: #DD0000">"127.0.0.1"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">8089</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;exit(</span><span style="color: #DD0000">"bind(2)&nbsp;failed\n"</span><span style="color: #007700">);<br />};<br /><br /></span><span style="color: #0000BB">$http</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setCallback</span><span style="color: #007700">(</span><span style="color: #DD0000">"/about"</span><span style="color: #007700">,&nbsp;function(</span><span style="color: #0000BB">$req</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"URI:&nbsp;"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$req</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getUri</span><span style="color: #007700">(),&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$req</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">sendReply</span><span style="color: #007700">(</span><span style="color: #0000BB">200</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"OK"</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"OK\n"</span><span style="color: #007700">;<br />});<br /><br /></span><span style="color: #0000BB">$base</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">dispatch</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程的输出类似于：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
Client:

$ nc 127.0.0.1 8088
GET /about HTTP/1.0
Connection: close

HTTP/1.0 200 OK
Content-Type: text/html; charset=ISO-8859-1
Connection: close

$ nc 127.0.0.1 8089
GET /unknown HTTP/1.0
Connection: close

HTTP/1.1 404 Not Found
Content-Type: text/html
Date: Wed, 13 Mar 2013 04:14:41 GMT
Content-Length: 149
Connection: close

&lt;html&gt;&lt;head&gt;&lt;title&gt;404 Not Found&lt;/title&gt;&lt;/head&gt;&lt;body&gt;&lt;h1&gt;Not Found&lt;/h1&gt;&lt;p&gt;The requested URL /unknown was not found on this server.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;

Server:
URI: /about
OK
</pre></div>
   </div>
  </div>
 </div>

 <div class="refsect1 seealso" id="refsect1-eventhttp.bind-seealso">
  <h3 class="title">参见</h3>
  <ul class="simplelist">
   <li class="member">
    <span class="methodname"><a href="eventhttp.accept.html" class="methodname" rel="rdfs-seeAlso">EventHttp::accept()</a> - Makes an HTTP server accept connections on the specified socket stream or resource</span>
   </li>
  </ul>
 </div>

</div></div></div></body></html>